package com.gua.jbs.serviceImpl;

import java.util.*;

import org.apache.log4j.Logger;

import com.gua.jbs.bean.ProductBean;
import com.gua.jbs.dao.IProductDao;
import com.gua.jbs.daoImpl.ProductDao;
import com.gua.jbs.pojo.Jbspit;
import com.gua.jbs.service.IProductService;
import com.gua.jbs.service.IStockService;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.ExceptionMessage;

/**
 * 
 * @author Piyush Service class for adding, editing and deleting a product from
 *         database
 * 
 */
public class ProductService extends BaseService implements IProductService {

	private IProductDao productDao = null;
	private IStockService stockService = null;

	public ProductService() {
		productDao = new ProductDao();
		stockService = new StockService();
	}

	private static final Logger logger = Logger.getLogger(ProductService.class);

	public String addProduct(ProductBean productBean)
			throws ApplicationException {
		String pk = null;
		if (productBean != null) {
			try {
				// Set the pojo to be persisted
				Jbspit objPit = new Jbspit();
				objPit.setProdName(productBean.getName());
				objPit.setProdColor(productBean.getColor());
				objPit.setProdSize(productBean.getSize());
				objPit.setProdPrice(productBean.getPrice());
				objPit.setProdCost(productBean.getCostPrice());
				objPit.setProdweight("0.0");
				objPit.setProdCode(productBean.getBarCode());
				objPit.setProdType(productBean.getType());
				objPit.setProdSubType(productBean.getSubtype());
				objPit.setProdImage(productBean.getImage());
				objPit.setIsApproved(productBean.getIsApproved());
				objPit.setActive(true);
				objPit.setCreateDate(new Date());
				objPit.setModifyDate(new Date());
				objPit.setExtrafield1(productBean.getDesc());
				objPit.setExtrafield2(productBean.getPolish());
				objPit.setExtrafield3(productBean.getQuantity());
				objPit.setCreatedBy("Yogesh");
				objPit.setModifiedBy("Yogesh");
				objPit.setDesignCode(productBean.getDesignCode());
				objPit.setHrCode(productBean.getHrCode());
				objPit.setProdSubType(productBean.getSubtype());

				pk = productDao.saveProduct(objPit);
				if (pk != null) {

					// While adding new product, initial quantity will be zero
					// user entered will be the current. Sum of both will be the
					// total stock
					String prevQuantity = "0";
					String currentQuantity = productBean.getQuantity();
					stockService.addStockForProduct(pk, prevQuantity,
							currentQuantity);
				}
			} catch (ApplicationException ae) {
				throw ae;
			} catch (Exception e) {
				logger.error("ProductService : addProduct() ----> "
						+ e.getMessage());
				throw new ApplicationException(
						ExceptionMessage.PRODUCT_SAVE_UNABLE);
			}
		}
		return pk;
	}

	public boolean updateProduct(ProductBean productBean, String prodId)
			throws ApplicationException {
		boolean isUpdateSaved = false;

		try {

			// Set the pojo to be persisted
			Jbspit objPit = new Jbspit();
			objPit.setProdId(productBean.getId());
			objPit.setProdName(productBean.getName());
			objPit.setProdColor(productBean.getColor());
			objPit.setProdSize(productBean.getSize());
			objPit.setProdPrice(productBean.getPrice());
			objPit.setProdCost(productBean.getCostPrice());
			objPit.setProdweight("0.0");
			objPit.setProdCode(productBean.getBarCode());
			objPit.setProdType(productBean.getType());
			objPit.setProdSubType(productBean.getSubtype());
			objPit.setProdImage(productBean.getImage());
			objPit.setIsApproved(productBean.getIsApproved());
			objPit.setActive(true);
			objPit.setCreateDate(new Date());
			objPit.setModifyDate(new Date());
			objPit.setExtrafield1(productBean.getDesc());
			objPit.setExtrafield2(productBean.getPolish());
			objPit.setExtrafield3(productBean.getQuantity());
			objPit.setCreatedBy("Yogesh");
			objPit.setModifiedBy("Yogesh");
			objPit.setDesignCode(productBean.getDesignCode());
			objPit.setHrCode(productBean.getHrCode());
			objPit.setProdSubType(productBean.getSubtype());

			isUpdateSaved = productDao.updateProduct(objPit);
			if (isUpdateSaved != false) {
				try {
					int availableQuantity = stockService
							.getProductStock(productBean.getId());
					stockService.addStockForProduct(prodId, ""
							+ availableQuantity, productBean.getQuantity());
				} catch (ApplicationException ae) {
					throw ae;
				}
			}
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("ProductService : updateProduct() ----> "
					+ e.getMessage());
			throw new ApplicationException(ExceptionMessage.PRODUCT_EDIT_UNABLE);
		}
		return isUpdateSaved;
	}

	public boolean deleteProduct(String prodId) throws ApplicationException {
		// TODO Auto-generated method stub
		boolean isDelete = false;
		try {
			isDelete = productDao.deleteProductDao(prodId);
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("ProductService : deleteProduct() ----> "
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.PRODUCT_DELETE_UNABLE);
		}
		return isDelete;
	}

	/**
	 * Method to get a product by its Bar-Code
	 */
	public ProductBean getProductByCode(String productCode)
			throws ApplicationException {
		ProductBean productBean = new ProductBean();
		Jbspit jbspit = null;
		try {
			jbspit = productDao.findProductByCode(productCode);
			productBean.setId(jbspit.getProdId());
			productBean.setName(jbspit.getProdName());
			productBean.setDesc(jbspit.getExtrafield1());
			productBean.setPrice(jbspit.getProdPrice());
		} catch (ApplicationException ae) {
			throw ae;
		} catch (Exception e) {
			logger.error("ProductService : deleteProduct() ----> "
					+ e.getMessage());
			throw new ApplicationException(
					ExceptionMessage.PRODUCT_DELETE_UNABLE);
		}
		return productBean;
	}

	@Override
	public List<Jbspit> getProductsByDateFilter(String fromDate, String toDate)
			throws ApplicationException {
		// TODO Auto-generated method stub
		IProductDao productDao = new ProductDao();
		List<Jbspit> list_products = new ArrayList<Jbspit>();
		try {
			list_products = productDao.filterProductsByDate(fromDate, toDate);
		} catch (Exception e) {
			logger.info("Exception in Product service " + e);
		}
		logger.info("Product service list_products" + list_products.size());
		return list_products;

	}

	@Override
	public Map<Integer, Map<String, String>> getProductsType()
			throws ApplicationException {
		// TODO Auto-generated method stub

		IProductDao productDao = new ProductDao();
		Map<Integer, Map<String, String>> map_prodType = new LinkedHashMap<Integer, Map<String, String>>();
		map_prodType = productDao.productsType();
		return map_prodType;
	}

	@Override
	public Map<Integer, Map<String, String>> getProductsSubType()
			throws ApplicationException {
		// TODO Auto-generated method stub
		IProductDao productDao = new ProductDao();
		Map<Integer, Map<String, String>> map_prodSubType = new LinkedHashMap<Integer, Map<String, String>>();
		map_prodSubType = productDao.productsSubType();
		return map_prodSubType;
	}

	@Override
	public Map<Integer, Map<String, String>> getProductsPolish()
			throws ApplicationException {
		// TODO Auto-generated method stub
		IProductDao productDao = new ProductDao();
		Map<Integer, Map<String, String>> map_prodPolish = new LinkedHashMap<Integer, Map<String, String>>();
		map_prodPolish = productDao.productsPolish();
		return map_prodPolish;
	}

	@Override
	public Map<Integer, Map<String, String>> getProductsColor()
			throws ApplicationException {
		// TODO Auto-generated method stub
		IProductDao productDao = new ProductDao();
		Map<Integer, Map<String, String>> map_prodColor = new LinkedHashMap<Integer, Map<String, String>>();
		map_prodColor = productDao.productsColor();
		return map_prodColor;
	}

	@Override
	public Map<Integer, Map<String, String>> getProductsSize()
			throws ApplicationException {
		// TODO Auto-generated method stub
		IProductDao productDao = new ProductDao();
		Map<Integer, Map<String, String>> map_prodSize = new LinkedHashMap<Integer, Map<String, String>>();
		map_prodSize = productDao.productsSize();
		return map_prodSize;
	}

	/**
	 * Method to get the latest value of bar code generator
	 */
	public String getGeneratorCounter() throws ApplicationException {
		IProductDao productDao = new ProductDao();
		String generatorCount = "";
		try {
			generatorCount = productDao.getBarCodeCounter();
		} catch (ApplicationException ae) {
			throw ae;
		}
		return generatorCount;
	}

	/**
	 * Method to update the generator value
	 * 
	 * @return
	 * @throws ApplicationException
	 */
	public boolean updateGeneratorCounter(String counter)
			throws ApplicationException {
		IProductDao productDao = new ProductDao();
		boolean isUpdated = false;

		try {
			isUpdated = productDao.updateGeneratorVal(counter);
		} catch (ApplicationException ae) {
			throw ae;
		}
		return isUpdated;
	}

}
